package com.example.demo.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.common.Result;
import com.example.demo.entity.Audit;
import com.example.demo.entity.Params;
import com.example.demo.entity.Type;
import com.example.demo.exception.CustomException;
import com.example.demo.service.AuditService;
import com.example.demo.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;


@CrossOrigin
@RestController
@RequestMapping("/audit")
public class AuditController {
        @Resource
        private AuditService auditService;

        @PostMapping
        public Result save(@RequestBody Audit audit) {
            if (audit.getId() == null) {
                auditService.add(audit);
                return Result.success();
            } else {
                auditService.update(audit);
                return Result.success();
            }
        }

        @GetMapping
        public Result findAll() {
            return Result.success(auditService.findAll());
        }

        @GetMapping("/search")
        public Result findBySearch(Params params) {
            PageInfo<Audit> homeworklist = auditService.findBySearch(params);
            return Result.success(homeworklist);
        }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            auditService.delete(id);
            return Result.success();
        }

        @PutMapping
        public Result update(@RequestBody Audit audit) {
            auditService.update(audit);
            return Result.success();

    }
}
